package mx.itesm.meteorlag.Interface;

import mx.itesm.meteorlag.ResourcesController;

import org.andengine.entity.Entity;
import org.andengine.entity.text.Text;
import org.andengine.util.adt.color.Color;

/**
 * Created by Adrian on 4/20/15.
 */
public class TextTimer extends Entity {

    public Text textTimer;
    public double secs = 0;
    public double mins = 0;

    public TextTimer(float pX, float pY, float pWidth, float pHeight) {
        super(pX, pY, pWidth, pHeight);

        this.textTimer = ResourcesController.getInstance().generateText("  00:00  ",this.getWidth()/2,this.getHeight()/2,80,0xFFFFFFFF);
        this.reset();
        this.attachChild(this.textTimer);
    }

    public void reset()
    {
        this.textTimer.setText("00:00");
    }


    public void turnBlack()
    {

        this.textTimer.setColor(Color.BLACK);

    }


    public void updateTimeInSecs(float secs)
    {

        boolean negative = false;
        if (secs<0) {
            negative = true;
            secs = Math.abs(secs-1);
        }



        this.mins = Math.floor(Math.floor(secs)/60.0);
        this.secs = secs - (60*this.mins);

        this.updateTime(negative);

    }

    private void updateTime(boolean neg) {

        Double dm = Math.floor(this.mins);
        String m = String.format("%01d", dm.intValue() );

        Double ds = Math.floor(this.secs);
        String s = String.format("%02d", ds.intValue() );

        String sign = "";

        if (neg)
        {
            sign = "-";
            this.textTimer.setColor(Color.GREEN);
        }

        this.textTimer.setText(sign+m+":"+s);
    }

}
